package java.wxw;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.wxw.*;
/**
 * @author songzihan
 * @create 2020--07--03
 */
public class Login extends Final {
    public static void login() throws IOException {
        String  loginName = null,loginPassword = null,loginCpassword = null;
        String text = null;
        String text1 = null;
        int num=0;
        int count=0;
        String []textArray = new String[50];
        String []textArray1 = new String[50];
        int findout=0;
        DirExist dirExist = new DirExist();
        String password = "123455";//由于找不到用户目录，暂且用这个来代替
        UserDir userDir = new UserDir();
        boolean flag =true;
        String decide= "";

        while (true){
            findout = 0;
            System.out.println("login name: ");
            Scanner scanner = new Scanner(System.in);
            if (scanner.hasNext()){
                loginName = scanner.next();
                //去除左右两端空格
                loginName = loginName.trim();
                while (loginName.startsWith("　")) {//这里判断是不是全角空格
                    loginName = loginName.substring(1, loginName.length()).trim();
                }
                while (loginName.endsWith("　")) {
                    loginName = loginName.substring(0, loginName.length() - 1).trim();
                }
            }
            FileInputStream fileInputStream = new FileInputStream("c://osfile//mfd//user.txt");//定义一个file对象，用来初始化FileReader
            byte []buff =new byte[1024];
            int len = 0;
            while ((len=fileInputStream.read(buff))!=-1){
                text = new String(buff,0,len);
            }
            fileInputStream.close();
            textArray=text.split(";");
            while(textArray[num]!=null){
                if(textArray[num].toUpperCase().equals(loginName.toUpperCase())){
                    findout = 1;
                    loginCpassword =textArray[num+1];
                }
                num+=2;
            }
            if (findout==1){
                System.out.println("loginPassword: ");
                Scanner in = new Scanner(System.in);
                if (in.hasNext()){
                    loginPassword = in.next();
                }
                if (loginCpassword.equals(loginPassword)){
                    userDir.setUserName(loginName);
                    userName=loginName.toUpperCase();
                    FileInputStream fileInputStream1 = new FileInputStream("c://osfile//mfd");

                    byte []buff1 =new byte[1024];
                    int len1 = 0;
                    while ((len1=fileInputStream1.read(buff1))!=-1){
                        text1 = new String(buff1,0,len1);
                    }
                    fileInputStream1.close();
                    textArray1=text1.split(";");
                    while(textArray1[num]!=null){
                        String string;
                        string="c://osfile//";
                        string=string+textArray1[num];
                        userDirs[count].setUserName(textArray1[num]);
                        FileInputStream fileInputStream2= new FileInputStream(string);
                        fileCount[count]=0;

                    }
                }else{
                    flag=true;
                    while (flag){
                        System.out.println("Login Failed!  Password Error.  Try Again(Y/N):");
                        decide = "";
                        Scanner in1 = new Scanner(System.in);
                        if (in1.hasNext()){
                            decide =  in1.next();
                        }
                        if (decide.equals("Y")){
                            flag=true;
                        }else if (decide.equals("N")){
                            flag=true;
                            return;
                        }
                    }
                }

            }else {
                System.out.println("New Password(<=8):");
                Scanner in2 = new Scanner(System.in);
                if (in2.hasNext()){
                    loginPassword =  in2.next();
                }
                System.out.println("Confirm Password(<=8):");
                Scanner in3 = new Scanner(System.in);
                if (in3.hasNext()){
                    loginCpassword =  in3.next();
                }
                if (loginCpassword.equals(loginPassword)){
                    //把内容写进去
                }else{
                    flag=true;
                    while (flag){
                        System.out.println("Login Failed!  Password Error.  Try Again(Y/N):");
                        decide = "";
                        Scanner in1 = new Scanner(System.in);
                        if (in1.hasNext()){
                            decide =  in1.next();
                        }
                        if (decide.equals("Y")){
                            flag=true;
                        }else if (decide.equals("N")){
                            flag=true;
                            return;
                        }
                    }
                }
            }


        }


    }
}
